<?php
session_start();
include_once("config/config.inc.php");

$act = $_POST["act"];
$user_id = $_SESSION["hotel"]["userid"];
$amount = $_POST["new_tendered1"];
 
$datetime = date("Y-m-d H:i:s");
$shift = "start";
$amount = $_POST["new_tendered1"];
$deno_1 = $_POST["txt1"];
$deno_5 = $_POST["txt5"];
$deno_10 = $_POST["txt10"];
$deno_20 = $_POST["txt20"];
$deno_50 = $_POST["txt50"];
$deno_100 = $_POST["txt100"];
$deno_500 = $_POST["txt500"];
$deno_1000 = $_POST["txt1000"];
$oic_user_id = $_POST["ddl_"."4"];
$to_user_id = $_POST["ddl_"."6"];

if($act == "Save")
{
	//$sql = "insert into cash_on_hand(user_id,amount,deno_1,deno_5,deno_10,deno_20,deno_50,deno_100,deno_500,deno_1000,datetime) 
	//	values('$user_id','$amount','$deno_1','$deno_5','$deno_10','$deno_20','$deno_50','$deno_100','$deno_500','$deno_1000','$datetime')";
	//mysql_query($sql);
	$newid = 0;
	$sql = "select shift from `shift-transactions` order by `shift-transaction_id` desc limit 0,1";
	$res = mysql_query($sql);
	$row = mysql_fetch_row($res);
	if($row[0]=='end') {
		$sql = "insert into `shift-transactions`(shift,user_id,oic_user_id,to_user_id,cash_on_hand_id,datetime) 
		values('start','$user_id','$oic_user_id','$to_user_id','$newid','$datetime')";
		mysql_query($sql);
	}	
	//echo "<span style='color:red'>Shift start has been saved.</span>";
	echo "<script>document.location.href='../index.php';</script>";
}


function getUserddl($group_id)
{
	$sql = "select user_id, fullname from users where group_id = '$group_id'";
	$res = mysql_query($sql);
	$opt = "<option value='0' ></option>";
	while(list($user_id, $fullname)=mysql_fetch_row($res)){
		$opt .= "<option value='$user_id'>$fullname</option>";		
	}
	$ret = "<select id='ddl_".$group_id."' name='ddl_".$group_id."' class='keyselect'>";
	$ret .=	$opt;
	$ret .= "</select>";
	
	return $ret;
}

$sql = "select shift from `shift-transactions`  order by datetime desc limit 0,1";
$res = mysql_query($sql);

$usershift="";
while(list($shift)=mysql_fetch_row($res))
{
 	$usershift = $shift;
}
$num = mysql_num_rows($res);

if($usershift == 'end' || $num == 0)
{
?>

<form name='myform'  id="myform" method='post'>
<table>
<tr>
<td>Shift OIC:</td>
<td>
	<? echo getUserddl("4") ?>
</td>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
	<td>Telephone Operator:</td>
	<td>
		<? echo getUserddl("6") ?>
	</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<!--tr>
	<td style="vertical-align:top">Cash on Hand:</td>
	<td>
		<table cellpadding=0 cellspacing=3  />
		<tr><td style="text-align:right">1</td>
			<td width="70"><input type="text" id="txt1" name="txt1"  size="5" class="keypadfield" style="text-align:right" /></td>
			<td><input type="text" id="txttotal1" name="txttxttotal1"  size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right">5</td>
			<td><input type="text" id="txt5" name="txt5" size="5" class="keypadfield" style="text-align:right"/></td>
			<td><input type="text" id="txttotal5" name="txttxttotal5"  size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right">10</td>
			<td><input type="text" id="txt10" name="txt10" size="5" class="keypadfield" style="text-align:right"/></td>
			<td><input type="text" id="txttotal10" name="txttxttotal10"  size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right">20</td>
			<td><input type="text" id="txt20" name="txt20" size="5" class="keypadfield" style="text-align:right"/></td>
			<td><input type="text" id="txttotal20" name="txttxttotal20"  size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right">50</td>
			<td><input type="text" id="txt50" name="txt50" size="5" class="keypadfield" style="text-align:right"/></td>
			<td><input type="text" id="txttotal50" name="txttxttotal50"  size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right">100</td>
			<td><input type="text" id="txt100" name="txt100" size="5" class="keypadfield" style="text-align:right"/></td>
			<td><input type="text" id="txttotal100" name="txttxttotal100"  size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right">500</td>
			<td><input type="text" id="txt500" name="txt500" size="5" class="keypadfield" style="text-align:right"/></td>
			<td><input type="text" id="txttotal500" name="txttotal500" size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right">1000</td>
			<td><input type="text" id="txt1000" name="txt1000" size="5" class="keypadfield" style="text-align:right"/></td>
			<td><input type="text" id="txttotal1000" name="txttotal1000" size="7" style="text-align:right;background-color: white" disabled /></td></tr>
		<tr><td style="text-align:right"><b>Total:</b></td>
			<td colspan="2"><input type="text"  class="money" id="new_tendered" name="new_tendered" value="0" size="16" style="text-align:right;background-color: white;font-weight:bold;" disabled />
			<input type="hidden" name="new_tendered1" id="new_tendered1" />
			</tr>
		<tr><td><input type="button" class="denomination" value="Clear" /></td></tr>
		</table>
	</td>
</tr-->
<tr>
	<td>&nbsp;</td>
	<td><input name="act" id="act" type="submit" value="Save" class="cmdbtn" /></td>
</tr>
</table>
</form>
<?

}
else
{
?>

<span class="error">You already have started your shift.</span-->

<?
}
?>
<style>
.cmdbtn {
	width:100px;
	border:1px solid  #ffffff;
	background-color: #9BD1E6;
	margin-right:2px;
	padding:2px;
	cursor:pointer;
}

.keypadfield{ border:1px solid #51B1D8; background-color:#E0EBEF; width:100%}
table {font-family:lucida,arial,helvetica;font-size:.9em}
</style>
<link rel="stylesheet" type="text/css" href="../css/jquery.keypad.css" />
<link rel="stylesheet" type="text/css" href="../css/start/jquery-ui.css" />
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/jquery.keypad.pack.js"></script>
<script type="text/javascript" src="../js/jquery-ui.js"></script>
<script lang="javascript">

$(document).ready(function(){
	$(".keypadfield").keypad({keypadOnly:true});
	//$(".keypadfield").onchange(function() {
	//	var _mulnewval = $($(this).val()).val()*1;
	//	 $($(this).name()).val( _mulnewval); //multiplier
	//});
	  	
	

	$(".keypadfield").change(function(event) {
		var _mulnewval = $(this).val()*1;	
		var _deno = $(this).attr('name').match(/\d+/)[0]*1;
		var _newtotval =  _mulnewval * _deno;
		$("#txttotal"+_deno).val(_newtotval)	

		var curval = $("#new_tendered").val() * 1;
		var arr = [ "txttotal1", "txttotal5", "txttotal10", "txttotal20", "txttotal50", "txttotal100", "txttotal500" ,  "txttotal1000"];
		var _ftotal = 0;
		
		jQuery.each(arr, function() {
			var _val = $("#" + this).val() * 1;
			_ftotal =  _val + _ftotal; 
		});
		$("#new_tendered").val(_ftotal);		
		$("#new_tendered1").val(_ftotal);
	});
	$(".denomination").click(function() {
		if($(this).val()=="Clear") {
			var arr = [ "txttotal1", "txttotal5", "txttotal10", "txttotal20", "txttotal50", "txttotal100", "txttotal500" ,  "txttotal1000"];
			jQuery.each(arr, function() {
				$("#"+this).val( "");
			});
			var _arr = [ "txt1", "txt5", "txt10", "txt20", "txt50", "txt100", "txt500" ,  "txt1000"];
			jQuery.each(_arr, function() {
				$("#"+this).val( "");
			});
			$("#new_tendered").val( 0);
			$("#new_tendered1").val( 0);
			
		}
	});
	/*$(".denomination").click(function() {
		if($(this).val()=="Clear") {
			$("#new_tendered").val( 0);
		}else{
			var _btnval = $(this).val()*1;
			var _mulcurval = $("#txt"+$(this).val()).val()*1;
			var _totcurval = $("#txttotal"+$(this).val()).val()*1;
			var _mulnewval = _mulcurval + 1;
			var _totnewval = _totcurval + _btnval;
			$("#txt"+($(this).val())).val( _mulnewval); //multiplier
			$("#txttotal"+($(this).val())).val(_totnewval); //total
			
			var curval = $("#new_tendered").val() * 1;
			var newval = $(this).val() * 1;
			$("#new_tendered").val( curval + newval);
	 	}
	});*/
	
	
	
});
</script>
